/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csci6450.chickenFriedFS.metaServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jacobschoen
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            System.out.println("Meta Server started");
            List<String> errors = new ArrayList<String>();
            //get any arguments passed in
            for (int i = 0; i < args.length; i = i + 2) {

                if (args[i].equals("-cp")) {
                    try {
                        RuntimeSettings.getInstance().setClientListenerPort(Integer.parseInt(args[i + 1]));
                    } catch (NumberFormatException numberFormatException) {
                        errors.add("-cp must be followed by a positive integer.");
                    }
                } else if (args[i].equals("-hb")) {
                    try {
                        RuntimeSettings.getInstance().setHearBeatWaitPeriod(Integer.parseInt(args[i + 1]) * 1000);
                    } catch (NumberFormatException numberFormatException) {
                        errors.add("-hb must be followed by a positive integer.");
                    }
                } else if (args[i].equals("-ml")) {
                    RuntimeSettings.getInstance().setMetaServerLogLocation(args[i + 1]);
                } else if (args[i].equals("-nslp")) {
                    try {
                        RuntimeSettings.getInstance().setNuggetServerListenerPort(Integer.parseInt(args[i + 1]));
                    } catch (NumberFormatException numberFormatException) {
                        errors.add("-nslp must be followed by a positive integer.");
                    }
                } else if (args[i].equals("-rp")) {
                    try {
                        RuntimeSettings.getInstance().setReplicationPort(Integer.parseInt(args[i + 1]));
                    } catch (NumberFormatException numberFormatException) {
                        errors.add("-nslp must be followed by a positive integer.");
                    }
                } else if (args[i].equals("-h")) {

                    //-cp client listener port
                    //-hb heart beat wait period
                    //-ml meta server log location
                    //-nslp nugget Server listener port
                    errors.add("Following is the list of arguments that can be used to start the Nugget Server:");
                    errors.add("  '-cp'   The Port that this Meta Server will listen for Client requests on.");
                    errors.add("  '-hb'   The number of seconds to wait before checks on Nugget Server.");
                    errors.add("  '-ml'   The location to store the Meta Server log.");
                    errors.add("  '-nslp' The Port that this Meta Server will listen for Nugget Servers wanting");
                    errors.add("          to join.");
                    errors.add("          WARNING: This will lose any data currently stored in this nugget");
                    errors.add("          server.");
                    errors.add("  '-rp'   The port that the META Server will listen for replication requests on.");
                    errors.add("  '-h'    This will print the information above.");
                }

            }

            try {
                java.net.InetAddress i = java.net.InetAddress.getLocalHost();
                System.out.println("Your parent OS is " + System.getProperty("os.name"));
                System.out.println("System IP address is: " + i.getHostAddress()); // IP address only
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (errors.size() == 0) {
                //means no problems so lets start this shindig
                MetaServer.getInstance().start();
                listenForClientInput();
                System.exit(0);
            } else {
                //mean something went wrong tell the user
                for (int i = 0; i < errors.size(); i = i + 1) {
                    System.out.println(errors.get(i));
                }
                System.exit(0);
            }

        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(0);
        }
    }

    private static void listenForClientInput() throws IOException {
        System.out.println("Welcome to the Meta Server. It has been successfully started.");
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromUser = new String();
        while (!fromUser.equalsIgnoreCase("EXIT")) {
            System.out.print("CFF$");
            fromUser = stdIn.readLine();
            if (!fromUser.equalsIgnoreCase("EXIT")) {
                System.out.println("CFFS does not recognize " + fromUser.trim() + ".");
            }
            System.out.println("Nugget Server has been shut down.");
        }

    }
}
